package cn.com.dpc;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.dpc.adapter.ColumnAdapter;
import cn.com.dpc.adapter.MixAdapter;
import cn.com.dpc.bean.Columnobject;
import cn.com.dpc.bean.User;
import cn.com.dpc.util.DataService;
import cn.com.dpc.util.HttpGet;

public class DevListActivity extends BaseListActivity {
	private int stationId;
	private String stationName = "";
	private String resultStr = "";

	private ColumnAdapter columnAdapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.subject);

		Bundle bundle = getIntent().getExtras();
		stationId = bundle.getInt("stationId");
		stationName = bundle.getString("stationName");

		ImageButton editButton = (ImageButton) findViewById(R.id.edit_button);
		editButton.setVisibility(View.INVISIBLE);
		ImageButton backButton = (ImageButton) findViewById(R.id.back_button);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				doExit();
			}
		});

		TextView titleText = (TextView) findViewById(R.id.myTitle);
		titleText.setText(stationName);

		showList();

	}

	private void showList() {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... arg0) {
				try {
					User user = DataService.queryUser(DevListActivity.this);
					resultStr = HttpGet.getResult("http://" + user.getIp()
							+ "/andriod!findDev.do?stationNo=" + stationId,
							DevListActivity.this, "utf8");
					return true;
				} catch (Exception e) {
					e.printStackTrace();
				}
				return false;
			}

			@Override
			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				closeProgressBar();
				if (result) {

					try {
						JSONObject jsonObject = new JSONObject(resultStr);
						JSONArray jsonArray = jsonObject.getJSONArray("datas");

						columnAdapter = new MixAdapter(DevListActivity.this);

						List<Columnobject> list = new ArrayList<Columnobject>();
						for (int i = 0; i < jsonArray.length(); i++) {
							JSONObject jo = jsonArray.getJSONObject(i);
							Columnobject columnobject = new Columnobject();
							columnobject.setNumber(1);
							columnobject.setType(jo.getInt("devNo"));
							columnobject.setUrl(jo.getString("devNo"));
							columnobject.setTexts(jo.getString("devName"));
							list.add(columnobject);
						}
						changeObject(list, columnAdapter);
						setListAdapter(columnAdapter);

					} catch (Exception e) {
						// TODO: handle exception
					}

				} else {
					Toast.makeText(DevListActivity.this, "数据加载失败！",
							Toast.LENGTH_SHORT).show();
				}

			}

			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				showProgressBar();
			}

		}.execute();

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Intent intent = new Intent(DevListActivity.this, ParamsActivity.class);
		intent.setFlags(1);
		Bundle bundle = new Bundle();
		bundle.putString("url", stationId + "_"
				+ columnAdapter.getUrl()[position]);
		bundle.putString("text", columnAdapter.getTexts()[position]);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	private void changeObject(List<Columnobject> list,
			ColumnAdapter columnAdapter) {
		int[] number = new int[list.size()];
		int[] type = new int[list.size()];
		String[] url = new String[list.size()];
		String[] texts = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			Columnobject columnobject = list.get(i);
			number[i] = columnobject.getNumber();
			type[i] = columnobject.getType();
			url[i] = columnobject.getUrl();
			texts[i] = columnobject.getTexts();
		}
		columnAdapter.setNumber(number);
		columnAdapter.setType(type);
		columnAdapter.setUrl(url);
		columnAdapter.setTexts(texts);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			doExit();
			return true;
		}
		return true;
	}
}
